version 10set more off// This calculates the distance weighted version of a given variable, by case. Within each// case, such as a year, the weighted version will equal the unweighted version plus// 1/(1+distance) times all the other unweighted values within the same case. It is critical// to choose well the unit of distance, the argument scale.// Arguments:// var   = variable to be weighted// x     = latitude// y     = longitude // byvar = by variable, defining a case, such as a year // scale = unit of distanceargs var x y byvar scale// First sort by the byvarquietly sort `byvar'quietly local n = _Nquietly local case = 1quietly gen `var'_wt`scale' = 0quietly while `case' < `n' { 	local min = `case'	while `byvar'[`case'] == `byvar'[`case'+1] {		local case = `case' + 1	}	local max = `case'	local i = `min'	while `i' <= `max' {		local j = `min'		local zwt = 0		while `j' <= `max' {			if `j'==`i' {				local zwt = `zwt' + `var'[`j']			}			else {				local a = abs(`y'[`i']-`y'[`j'])				local zwt = `zwt' + `var'[`j'] / (1 + (1/`scale') * 3959.8712 /*				*/	    * acos(sin(`x'[`i']) /*				*/      * sin(`x'[`j']) + cos(`x'[`i']) * cos(`x'[`j'])  /*				*/	    * cos(`a')))			}			local j = `j' + 1		}		quietly replace `var'_wt`scale' = `zwt' if _n == `i'		local i = `i' + 1	}	local case = `case' + 1}